/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.mrj;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.UIManager;

import org.isqlviewer.sql.embedded.EmbeddedDatabase;
import org.isqlviewer.swing.action.SharedActions;
import org.isqlviewer.swing.action.SwingEventManager;
import org.isqlviewer.ui.ApplicationFrame;
import org.isqlviewer.util.IsqlToolkit;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;
import com.apple.eio.FileManager;

/**
 * Launcher class for starting the iSQL-Viewer application on Mac OS X.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class AquaLauncher extends ApplicationAdapter {

    public static final short kSystemDomain = -32766;
    public static final short kLocalDomain = -32765;
    public static final short kNetworkDomain = -32764;
    public static final short kClassicDomain = -32762;
    public static final short kUserDomain = -32763;

    private SwingEventManager eventManager = null;

    public AquaLauncher(SwingEventManager eventManager) {

        this.eventManager = eventManager;
    }

    public static void main(String[] args) {

        String applicationSupport = null;
        try {
            short domain = Domain.USER.value();
            int folderType = FolderType.APPLICATION_SUPPORT.value();
            applicationSupport = FileManager.findFolder(domain, folderType, true);
        } catch (FileNotFoundException error) {
            error.printStackTrace();
            return;
        }
        File homeDirectory = new File(applicationSupport, "iSQL-Viewer");
        homeDirectory.mkdirs();
        System.setProperty(IsqlToolkit.PROPERTY_MRJ_ENABLED, "true");
        System.setProperty(IsqlToolkit.PROPERTY_HOME, homeDirectory.getAbsolutePath());
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "iSQL-Viewer");
        System.setProperty("com.apple.mrj.application.live-resize", "true");

        Application appliactionInstance = Application.getApplication();
        appliactionInstance.setEnabledAboutMenu(true);
        appliactionInstance.setEnabledPreferencesMenu(false);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable error) {
            error.printStackTrace();
            return;
        }
        // ensure that the embedded database driver is available //
        EmbeddedDatabase.getSharedInstance();
        ApplicationFrame frame = new ApplicationFrame();
        appliactionInstance.addApplicationListener(new AquaLauncher(frame.getSwingEventManager()));
        frame.setVisible(true);
    }

    @Override
    public void handleAbout(ApplicationEvent event) {

    }

    @Override
    public void handleOpenApplication(ApplicationEvent event) {

    }

    @Override
    public void handleOpenFile(ApplicationEvent event) {

    }

    @Override
    public void handlePreferences(ApplicationEvent event) {

    }

    @Override
    public void handlePrintFile(ApplicationEvent event) {

    }

    @Override
    public void handleQuit(ApplicationEvent event) {

        ActionEvent action = new ActionEvent(event.getSource(), SharedActions.EXIT_APPLICATION, "");
        eventManager.actionPerformed(action);
    }

    @Override
    public void handleReOpenApplication(ApplicationEvent event) {

    }
}